# 第一阶段：构建环境（使用与服务器兼容的ARM64基础镜像）
FROM arm64v8/centos:7 AS builder

# 设置与服务器一致的环境变量
# Sets the default system language environment to UTF-8 encoded English (United States) locale
ENV LANG=en_US.UTF-8 

# 步骤1：更换国内源
RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup && \
    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-altarch-7.repo && \
    sed -i 's/\$basearch/aarch64/g' /etc/yum.repos.d/CentOS-Base.repo && \
    yum clean all && \
    yum makecache

# 步骤2：安装基础工具
RUN yum install -y \
    make gcc-c++\
    perl perl-core \
    zlib bzip2 xz libffi\
    mesa-libGL \
    wget sudo which net-tools

COPY  additional_deps/installed_deps /opt/local/installed_deps



# 步骤4：安装GCC 9.2.0
RUN  cd /tmp &&\
    # wget -nc https://mirrors.aliyun.com/gnu/gcc/gcc-9.2.0/gcc-9.2.0.tar.gz &&\
    # tar xvf gcc-9.2.0.tar.gz &&\
    # cd gcc-9.2.0 &&\
    # cd .. &&\
    # mkdir gcc-build &&\   
    # cd gcc-build && ../gcc-9.2.0/configure \
    # --prefix=/opt/local/gcc-9.2 \
    # --enable-languages=c,c++ \
    # --with-system-zlib \
    # --disable-multilib \
    # --disable-bootstrap \
    # --with-arch=armv8-a &&\
    # make -j$(nproc) &&\
    # make install &&\
    # echo 'export PATH=/opt/local/gcc-9.2/bin:$PATH' | tee /etc/profile.d/gcc.sh &&\
    # echo 'export LD_LIBRARY_PATH=/opt/local/gcc-9.2/lib64:$LD_LIBRARY_PATH' | tee -a /etc/profile.d/gcc.sh &&\
    # source /etc/profile.d/gcc.sh &&\
    # 复制到系统库目录
    cp /opt/local/installed_deps/gcc-9.2/lib64/libstdc++.so.6.0.27 /usr/lib64/ &&\
    ln -sf /usr/lib64/libstdc++.so.6.0.27 /usr/lib64/libstdc++.so.6 



# 步骤3：安装openssl 到标准目录
RUN ln -sf /opt/local/installed_deps/openssl/bin/openssl /usr/bin/openssl &&\
    echo '/opt/local/installed_deps/openssl/lib' | tee /etc/ld.so.conf.d/openssl-1.1.1k.conf &&\
    ldconfig
    
# 临时解决OpenSSL依赖库问题
# export LD_LIBRARY_PATH="/opt/local/openssl/lib:$LD_LIBRARY_PATH"

# 设置工作目录
WORKDIR /app

# 设置非root用户（匹配服务器账号）
RUN groupadd -g 1000 txtdeal && \
    useradd -u 1000 -g 1000 -m txtdeal && \
    echo "txtdeal ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers && \
    echo "txtdeal ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/90-cloud-init-users && \
    chmod 0440 /etc/sudoers.d/90-cloud-init-users && \
    chown -R txtdeal:txtdeal /app && \
    chmod -R 755 /app

# 复制部署包到 app目录
# COPY source dest



